#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 4e5 + 10;
const int B = 630;

int xl[N], xr[N], id[N], a[N], n, Q;
priority_queue < int, vector < int >, greater < int > > q[N / B + 3];
priority_queue < int > p[N / B + 3];

void rebuild(int id, int l, int r, int &x) {
	rep(i, xl[id], xr[id]) q[id].push(a[i]), a[i] = q[id].top(), q[id].pop();
	rep(i, l, r) if(a[i] > x) swap(a[i], x);
	priority_queue < int, vector < int >, greater < int > >().swap(q[id]);
	priority_queue < int >().swap(p[id]);
	rep(i, xl[id], xr[id]) p[id].push(a[i]);
}

void solve(int l, int r, int &x) {
	if(id[l] == id[r]) rebuild(id[l], l, r, x);
	else {
		rebuild(id[l], l, xr[id[l]], x);
		rep(i, id[l] + 1, id[r] - 1) {
			q[i].push(x); int y = p[i].top(); p[i].pop();
			if(y > x) swap(x, y); p[i].push(y);
		}
		rebuild(id[r], xl[id[r]], r, x);
	}
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in, Q = in; rep(i, 1, n) a[i] = in;
	rep(i, 1, n) {
		id[i] = (i - 1) / B + 1; xl[id[i]] = xl[id[i]] ? xl[id[i]] : i; xr[id[i]] = i; p[id[i]].push(a[i]);
	}
	while(Q--) {
		int l = in, r = in, x = in;
		if(l <= r) solve(l, r, x); else solve(l, n, x), solve(1, r, x); printf("%d\n", x);
	} return 0;
}
